package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.SysPtExpAddBo;
import com.ruoyi.system.bo.SysPtExpQueryBo;
import com.ruoyi.system.bo.SysPtExpEditBo;
import com.ruoyi.system.domain.SysPtExp;
import com.ruoyi.system.mapper.SysPtExpMapper;
import com.ruoyi.system.vo.SysPtExpVo;
import com.ruoyi.system.service.ISysPtExpService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实验管理Service业务层处理
 *
 * @author 莫修
 * @date 2021-11-28
 */
@Service
public class SysPtExpServiceImpl extends ServiceImpl<SysPtExpMapper, SysPtExp> implements ISysPtExpService {

    @Override
    public SysPtExpVo queryById(Long expId){
        return getVoById(expId, SysPtExpVo.class);
    }

    @Override
    public TableDataInfo<SysPtExpVo> queryPageList(SysPtExpQueryBo bo) {
        PagePlus<SysPtExp, SysPtExpVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPtExpVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPtExpVo> queryList(SysPtExpQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPtExpVo.class);
    }

    private LambdaQueryWrapper<SysPtExp> buildQueryWrapper(SysPtExpQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPtExp> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), SysPtExp::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), SysPtExp::getContent, bo.getContent());
        lqw.eq(bo.getIsEnable() != null, SysPtExp::getIsEnable, bo.getIsEnable());
        lqw.eq(bo.getExpVideoNum() != null, SysPtExp::getExpVideoNum, bo.getExpVideoNum());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPtExpAddBo bo) {
        SysPtExp add = BeanUtil.toBean(bo, SysPtExp.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPtExpEditBo bo) {
        SysPtExp update = BeanUtil.toBean(bo, SysPtExp.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPtExp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
